<?php
class GoodsDetailesModel extends Model {
	public $table = 'goods_detailes';

	
	/**
	 * 重组商品详细表返回一个数组
	 */
	 public function goodsDetail($gid,$data){
	 	//用于保存新的数组
	 	$details = array();
	 	//将小图转为一个字符串
	 	//如果存在商品图片
	 	if(isset($data['imgs'])){
	 		//大图
	 		$details['bigimg'] = array();
			//小图
			$details['smallimg'] = array();
			//中图
			$details['mediumimg'] = array(); 
			foreach($data['imgs'] as $k=>$img){
				$details['bigimg'][] = $img.'_bigimg.'.$data['ext'][$k];
				$details['smallimg'][] = $img.'_small.'.$data['ext'][$k];
				$details['mediumimg'][] = $img.'_medium.'.$data['ext'][$k];
			}
			$details['bigimg'] = serialize($details['bigimg']);
			$details['smallimg'] = serialize($details['smallimg']);			
			$details['mediumimg'] = serialize($details['mediumimg']);
	 	}
		
		//如果存在售后服务
		if(isset($data['service'])){
			$details['service'] = $data['service'];
		}
		//商品详细信息
		$details['intro'] = $data['intro'];
		//商品ID
		$details['goods_gid'] = $gid;
		return K('GoodsDetailes')->add($details);
	 }
	 
	 /**
	  * 更新商品详细表
	  */
	 public function updateGoodsDetails($gid,$data){
	 	//用于保存新的数组
	 	$details = array();
	 	//将小图转为一个字符串
	 	//如果存在商品图片
	 	if(isset($data['imgs'])){
	 		//大图
	 		$details['bigimg'] = array();
			//小图
			$details['smallimg'] = array();
			//中图
			$details['mediumimg'] = array(); 
			foreach($data['imgs'] as $k=>$img){
				$details['bigimg'][] = $img.'_bigimg.jpg';
				$details['smallimg'][] = $img.'_small.jpg';
				$details['mediumimg'][] = $img.'_medium.jpg';
			}
			$details['bigimg'] = serialize($details['bigimg']);
			$details['smallimg'] = serialize($details['smallimg']);			
			$details['mediumimg'] = serialize($details['mediumimg']);
	 	}
		
		//如果存在售后服务
		if(isset($data['service'])){
			$details['service'] = $data['service'];
		}
		//商品详细信息
		$details['intro'] = $data['intro'];
		//商品ID
		$details['goods_gid'] = $gid;
		return K('GoodsDetailes')->where("goods_gid={$gid}")->update($details);
	 }
	
	/**
	 * 获得相应的商品的详细 信息将图片地址转为 一个数组返回来
	 */
	 public function GoodsDetails($gid){
	 	$details = $this->where("goods_gid={$gid}")->find();
		foreach($details as $key=>$val){
			if($key=='smallimg'){
				$details[$key] = array_reverse(unserialize($val));
			}
			if($key=='mediumimg'){
				$details[$key] = array_reverse(unserialize($val));
			}
			if($key=='bigimg'){
				$details[$key] = array_reverse(unserialize($val));
			}
		}
			return $details;
	 }
			
	}
